using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using TickSharp.Core.Interfaces;
using TickSharp.Core.Model;

namespace TickSharp.Core.Infrastructure.Serializers
{
    public class TaskSerializer : ISerializer<Task>
    {
        public IEnumerable<Task> Deserialize(XElement elements)
        {
            return from e in elements.Elements()
            let dateClosed = e.Element("closed_on").Value
            select new Task
            {
                Id = int.Parse(e.Element("id").Value),
                Name = e.Element("name").Value,
                Position = int.Parse(e.Element("position").Value),
                ProjectId = int.Parse(e.Element("project_id").Value),
                DateOpened = DateTime.Parse(e.Element("opened_on").Value),
                DateClosed = string.IsNullOrEmpty(dateClosed) ? (DateTime?)null : DateTime.Parse(dateClosed),
                Budget = decimal.Parse(e.Element("budget").Value),
                IsBillable = bool.Parse(e.Element("billable").Value)
            };
        }
    }
}